package com.chenfan.mcn.extension.excel;

import cn.afterturn.easypoi.entity.BaseTypeConstants;
import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import cn.afterturn.easypoi.excel.export.styler.ExcelExportStylerDefaultImpl;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.*;

import java.math.BigDecimal;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 自定义Excel样式
 *
 * @author wulg
 * @date 2021-10-26
 **/
public class CustomExcelExportStyler extends ExcelExportStylerDefaultImpl {

    /**
     * 不同的Workbook 不允许共享CellStyle 必须非静态
     */
    private Map<String, CellStyle> formatStayleMap = new ConcurrentHashMap<>();

    private final DataFormat dataFormat;

    private final CellStyle numberCellStyle;

    private final String number_style = "0.00_ ";

    public CustomExcelExportStyler(Workbook workbook) {
        super(workbook);
        dataFormat = workbook.createDataFormat();
        //设置格式为数值类型（"0.00_ ）及货币样式（#,##0.00）
        numberCellStyle = this.getCellStyle(number_style);
    }


    @Override
    public CellStyle getTitleStyle(short color) {
        CellStyle titleStyle = workbook.createCellStyle();
        titleStyle.setAlignment(HorizontalAlignment.CENTER);
        titleStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        titleStyle.setWrapText(true);
        Font font = workbook.createFont();
        font.setBold(true);
        titleStyle.setFont(font);
        return titleStyle;
    }

    private CellStyle getCellStyle(String format) {
        if(!formatStayleMap.containsKey(format)) {
            CellStyle cellStyle = workbook.createCellStyle();
            cellStyle.setAlignment(HorizontalAlignment.CENTER);
            cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
            cellStyle.setDataFormat(dataFormat.getFormat(format));
            cellStyle.setWrapText(true);
            formatStayleMap.putIfAbsent(format, cellStyle);
        }
        return formatStayleMap.get(format);
    }

    @Override
    public CellStyle getStyles(boolean noneStyler, ExcelExportEntity entity) {
        if(entity != null) {
            //依赖CustomExcelExportService
            CellStyle cellStyle = this.customCellStyle(entity);
            if(cellStyle != null) {
                return cellStyle;
            }
        }
        return super.getStyles(noneStyler, entity);
    }

    /**
     * 定制CellStyle
     * @param entity
     * @return
     */
    private CellStyle customCellStyle(ExcelExportEntity entity) {
        CellStyle cellStyle = null;
        if(entity.getMethod() != null) {
            Class<?> fieldType = entity.getMethod().getReturnType();
            //默认格式化
            if (BigDecimal.class.isAssignableFrom(fieldType) && BaseTypeConstants.DOUBLE_TYPE.equals(entity.getType())) {
                if(StringUtils.isBlank(entity.getNumFormat())) {
                    cellStyle = numberCellStyle;
                }
            }
        }
        return cellStyle;
    }
}
